# $Id: bashrc 88 2006-10-10 20:28:15Z cmauch $
#
# Author: Charles Mauch (charles@mauch.name)
# Description: ~/.bashrc: executed by bash for non-login shells.
# Created: Sun 02 Oct 2005 10:50:33 PM PDT
# Legal: (c) 2005 Charles Mauch, released under the terms of the GPLv2
#

#export LANG=en_US.ISO-8859-1

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# Check and mount fusesmb
#if [ ! -d ${HOME}/net/WORKGROUP ]; then
# fusesmb ${HOME}/net
#fi

# Check and mount encfs
#if [ -f ${HOME}/.crypt/.encfs5 ]; then
# if [ ! -f ${HOME}/crypt/mounted ]; then
#   encfs ~/.crypt ~/crypt
# fi
#fi

# Check for mplayer fifo, if it doesn't exist - create it
if [ ! -p /tmp/mplayer.fifo ]; then
 mkfifo /tmp/mplayer.fifo
fi


if [ $COLORSCHEME == "color" ]; then
 # set a fancy prompt
 if [ -f ~/dotfiles/bash/colorprompt.sh ]; then
     source ~/dotfiles/bash/colorprompt.sh
 fi
 # enable color support of ls
 if [ "$TERM" != "dumb" ]; then
     eval "`dircolors -b`"
 fi
 alias ls='ls --color=auto -hFs'
else
 if [ -f ~/dotfiles/bash/monoprompt.sh ]; then
     source ~/dotfiles/bash/monoprompt.sh
 fi
 alias ls='ls --color=never -hFs'
fi

# Define your own aliases here ...
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion
#fi

# Print banner if it exists
if [ -f ~/dotfiles/bash/banner ]; then
    clear
    cat ~/dotfiles/bash/banner
    echo
fi

# Fire up Gentoo's keychain manager.  Far better than hack I used
# before

if [ -f ~/.ssh/id_rsa ]; then
 ~/bin/keychain ~/.ssh/id_rsa -q
 [[ -f $HOME/.keychain/$HOSTNAME-sh ]] && . $HOME/.keychain/$HOSTNAME-sh
 [[ -f $HOME/.keychain/$HOSTNAME-sh-gpg ]] && . $HOME/.keychain/$HOSTNAME-sh-gpg
fi

if [ -f ~/.ssh/id_dsa ]; then
 ~/bin/keychain ~/.ssh/id_dsa -q
 [[ -f $HOME/.keychain/$HOSTNAME-sh ]] && . $HOME/.keychain/$HOSTNAME-sh
 [[ -f $HOME/.keychain/$HOSTNAME-sh-gpg ]] && . $HOME/.keychain/$HOSTNAME-sh-gpg
fi


# Fire up personal calendaring program, show current status for the
# next week.
if [ -f /usr/bin/when ]; then
 if [ -d ~/.when ]; then
     echo "      ..:: You have the following events happening in the next week ::.."
     echo
     when i --future=7 --past=0
     echo
 fi
fi

# define a starting line in our bash_history so we can save only new lines 
# and make sure that we append to a history file.
export CURBASHSTART=`grep -v "^[ \t]*$" $HISTFILE | wc -l | awk '{print $1}'`  CURBASHDATE=`date`
shopt -s cmdhist histappend

# Use Vi Shortcuts in bash (awesome!)
set -o vi

# Function to create a bash history archive
archive_history()
{
    HISTORYOLD=${HISTFILE}.archive
    CURTIME=`date`
    CURTTY=`tty`
    if  [ x$HISTDUMPPED = x ]; then
      echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ----" >>   $HISTORYOLD
      history $(($HISTCMD-${CURBASHSTART-0})) | sed -e 's/^[ ]*[0-9][0-9]* [ ]*//g'  >> $HISTORYOLD
      export HISTDUMPPED=1
    fi
}

# On exit, be sure to run function
exit ()
{
   archive_history
   builtin exit
}


trap 'archive_history' EXIT
